package top.suven.base.http.admin.log.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.suven.base.common.excel.ColumnBox;
import top.suven.base.common.excel.ExportUtils;
import top.suven.base.core.mybatis.PageData;
import top.suven.base.http.admin.frame.config.ResponseResult;
import top.suven.base.http.admin.log.model.LoginLog;
import top.suven.base.http.admin.log.service.LoginLogWebService;
import top.suven.base.http.admin.log.vo.request.LoginLogRequestVo;
import top.suven.base.http.admin.log.vo.response.LoginLogResponseVo;
import top.suven.base.http.admin.log.vo.valid.LogValidGroup;
import top.suven.base.http.enums.EnableStatusEnum;
import top.suven.base.http.enums.StatusEnum;
import top.suven.base.http.enums.SysMsgEnumType;
import top.suven.base.util.utilize.BeanUtil;
import top.suven.base.util.utilize.DateUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**   
 * @Title: LoginLogWebController.java
 * @Description: 登录日志的控制服务类
 * @author lixiangling
 * @date   2018/08/28 11:12:22
 * @version V1.0
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *登录日志
 * ----------------------------------------------------------------------------
 *
 */
@Controller
@RequestMapping("/log/loginLog")
public class LoginLogWebController {
    private final static Logger logger = LoggerFactory.getLogger(LoginLogWebController.class);
	@Autowired
	private LoginLogWebService loginLogWebService;
    /**
     * @Title: 跳转到登录日志主界面
     * @Description:loginLogRequestVo @{Link LoginLogRequestVo}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/28 11:12:22
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "")
    @RequiresPermissions("log:loginLog_list")
    public String index(){
        return "log/loginLog_index";
    }
    /**
     * @Title: 获取登录日志分页信息
     * @Description:loginLogRequestVo @{Link LoginLogRequestVo}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/28 11:12:22
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping(value = "list")
    @RequiresPermissions("log:loginLog_list")
    public PageData<LoginLogResponseVo> list(@Validated(value = {LogValidGroup.LogQueryGroup.class}) LoginLogRequestVo loginLogRequestVo){
        LoginLog loginLog = new LoginLog();
        BeanUtil.copyProperties(loginLogRequestVo, loginLog);
        return loginLogWebService.getLoginLogPage(loginLog, loginLogRequestVo.getPage(), loginLogRequestVo.getLimit());
    }
   /**
    * @Title: 新增登录日志信息
    * @Description:loginLogRequestVo @{Link LoginLogRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 11:12:22
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "add")
    @RequiresPermissions("log:loginLog_add")
    public ResponseResult add(@Validated(value = {LogValidGroup.LogFormGroup.class}) LoginLogRequestVo loginLogRequestVo){
        LoginLog loginLog = new LoginLog();
        BeanUtil.copyProperties(loginLogRequestVo, loginLog);
        loginLog.setStatus(StatusEnum.VALID.getIndex());
        loginLog.setEnableStatus(EnableStatusEnum.ENABLE.getIndex());
        loginLogWebService.addLoginLog(loginLog);
        return ResponseResult.ok();
    }
    /**
    * @Title: 修改登录日志信息
    * @Description:loginLogRequestVo @{Link LoginLogRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 11:12:22
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "modify")
    @RequiresPermissions("log:loginLog_modify")
    public ResponseResult modify(@Validated(value = {LogValidGroup.LogFormGroup.class}) LoginLogRequestVo loginLogRequestVo){
        LoginLog loginLog = new LoginLog();
        BeanUtil.copyProperties(loginLogRequestVo, loginLog);
        if(loginLog.getLogId() == null){
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_UPD_INFO_NO_EXIST);
        }
        loginLogWebService.updLoginLog(loginLog);
        return ResponseResult.ok();
    }
   /**
    * @Title: 查看登录日志信息
    * @Description:loginLogRequestVo @{Link LoginLogRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 11:12:22
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping(value = "detail")
    @RequiresPermissions("log:loginLog_list")
    public String detail(ModelMap modelMap, Long id){
        LoginLog loginLog = null;
        if(id > 0){
            loginLog = loginLogWebService.getLoginLog(id);
        }
        modelMap.put("loginLog", loginLog);
        return "log/loginLog_detail";
    }
    /**
     * @Title: 跳转登录日志编辑界面
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/28 11:12:22
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "edit")
    @RequiresPermissions("log:loginLog_modify")
    public String edit(ModelMap modelMap, @RequestParam(defaultValue = "0") Long id){
        LoginLog loginLog = null;
        if(id > 0){
            loginLog = loginLogWebService.getLoginLog(id);
        }
        modelMap.put("loginLog", loginLog);
        return "log/loginLog_edit";
    }
    /**
    * @Title: 跳转登录日志新增编辑界面
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 11:12:22
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping(value = "newInfo")
    @RequiresPermissions("log:loginLog_add")
    public String newInfo(ModelMap modelMap){
        return "log/loginLog_edit";
    }
    /**
     * @Title: 删除登录日志信息
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/28 11:12:22
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping("del")
    @RequiresPermissions("log:loginLog_del")
    public ResponseResult del( @RequestParam(value = "id") Long[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        loginLogWebService.del(ids);
        return ResponseResult.ok();
    }
   /**
    * @Title: 启用登录日志信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 11:12:22
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "turnOn")
    @RequiresPermissions("log:loginLog_turnOn")
    public ResponseResult turnOn(@RequestParam(value = "id") Long[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        loginLogWebService.turnOn(ids);
        return ResponseResult.ok();
    }
   /**
    * @Title: 禁用登录日志信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 11:12:22
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "turnOff")
    @RequiresPermissions("log:loginLog_turnOff")
    public ResponseResult turnOff(@RequestParam(value = "id") Long[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        loginLogWebService.turnOff(ids);
        return ResponseResult.ok();
    }

    /**
    * @Title: 导出登录日志信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 11:12:22
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping("export")
    @RequiresPermissions("log:loginLog_export")
    public void export(HttpServletResponse response, LoginLogRequestVo loginLogRequestVo){
        LoginLog loginLog = new LoginLog();
        BeanUtil.copyProperties(loginLogRequestVo, loginLog);
        PageData<LoginLogResponseVo> loginLogPage = loginLogWebService.getLoginLogPage(loginLog, loginLogRequestVo.getPage(), loginLogRequestVo.getLimit());
        List<LoginLogResponseVo> data = loginLogPage.getData();

        ColumnBox columnBox = ColumnBox.create()
                
			.add("logId", "日志标识")
			.add("logType", "日志类型")
			.add("userIdTmp", "操作员工号/会员")
			.add("loginTime", "登录时间")
			.add("logoutTime", "退出时间")
			.add("loginType", "登录方式 网页、客户端（android 、IOS、PC）")
			.add("ipAddress", "IP地址")
			.add("macId", "MAC地址")
			.add("creater", "创建人")
			.add("createTime", "创建时间")
			.add("modifier", "修改人")
			.add("modifyTime", "修改时间")
			.add("reorder", "排序")
			.add("remark", "备注")
			.add("enableStatus", "启用状态 启用-1、禁用-0")
			.add("userId", "用户ID")
			.add("clientType", "登录系统类型 pc android ios")
			.add("imei", "IMEI号")
			.add("mobileModel", "手机型号")
			.add("osType", "访问系统 WIN7 WIN8 XP ANDROID IOS")
			.add("userSource", "客户端编号")
			.add("version", "")
			.add("cuid", "cuid");

        try {
            ExportUtils.write(String.format("登录日志_%s.xls",DateUtil.date("yyyy-MM-dd",new Date().getTime())), response, data, columnBox);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}